#region  Summary
// ===========================================================================
// AUTHOR		 : Steven Shi 
// CREATE DATE	 : 10/24/2006
// PURPOSE		 : TSLCM 
// SPECIAL NOTES: 

// ===========================================================================
// Change History:
//	10/24/2006	Created
//  03/06/2007  Modified by Steven Shi for bug 2940(eliminate the web application scope feature)
//  25/02/2008  Modified by Robin Meure to make use of 'better' OM objects
// ===========================================================================
#endregion


using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace zevenseas.LifeCycleManagement.EventReceivers
{
    /// <summary>
    /// A  feature event handler,used to install other reveiver handler"Delete event receiver"
    /// When this feature is active,delete event receiver will be installed and activated
    /// When user create a new site collection, event hander will be registered into the relevant site
    /// </summary>
    public class DeleteCaptureFeatureCallOut : SPFeatureReceiver
    {

        #region Public Methods

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            //=================================================================================
            //Modified by Steven Shi for bug 2940(Mar-06-2007)
            //These codes are for First version, reserved it for feature request change
            //if (properties.Feature.Parent.GetType().ToString() == "Microsoft.SharePoint.Administration.SPWebApplication")
            //{
            //    System.Object currentApp = properties.Feature.Parent;
            //    SPWebApplication WebApp = (SPWebApplication)currentApp;
            //    foreach (SPSite spSite in WebApp.Sites)
            //        foreach (SPWeb spWeb in spSite.AllWebs)
            //            RegisterEventReceiver(spWeb);

            //}
            //else {
            //==================================================================================
            System.Object currentWeb = properties.Feature.Parent;
            SPWeb spWeb = (SPWeb)currentWeb;
            RegisterEventReceiver(spWeb);
            //}

        }
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            System.Object currentWeb = properties.Feature.Parent;
            SPWeb spWeb = (SPWeb)currentWeb;
            UnRegisterEventReceiver(spWeb);
        }
        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            return;
        }
        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            return;
        }
        #endregion

        #region Private Methods
        private void RegisterEventReceiver(SPWeb web)
        {
            string assemblyName = "zevenseas.LifeCycleManagement.EventReceivers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4306074270f0265a";
            string sequenceNumber = "10022";
            //If current web is a team site,newReceiver.Type = SPEventReceiverType.SiteDeleting;
            //Otherwise is SPEventReceiverType.WebDeleting;
            if (web.IsRootWeb == true)
            {
                SPEventReceiverDefinition newReceiver = web.EventReceivers.Add();
                newReceiver.Class = "zevenseas.LifeCycleManagement.EventReceivers.DeleteCapture";
                newReceiver.Assembly = assemblyName;
                newReceiver.SequenceNumber = Convert.ToInt32(sequenceNumber);
                newReceiver.Type = SPEventReceiverType.SiteDeleting;
                newReceiver.Update();
            }
            else
            {
                SPEventReceiverDefinition newReceiver = web.EventReceivers.Add();
                newReceiver.Class = "zevenseas.LifeCycleManagement.EventReceivers.DeleteCapture";
                newReceiver.Assembly = assemblyName;
                newReceiver.SequenceNumber = Convert.ToInt32(sequenceNumber);
                newReceiver.Type = SPEventReceiverType.WebDeleting;
                newReceiver.Update();
            }

        }
        private void UnRegisterEventReceiver(SPWeb web)
        {
            string sequenceNumber = "10022";
            foreach (SPEventReceiverDefinition EventReceiver in web.EventReceivers)
            {
                if (EventReceiver.SequenceNumber == Convert.ToInt32(sequenceNumber))
                {
                    SPEventReceiverDefinition DeleteReceiver = web.EventReceivers[EventReceiver.Id];
                    DeleteReceiver.Delete();
                }
            }


        }
       
        #endregion
    }
}
